using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace Framework
{
    public abstract class Singleton<T> where T : Singleton<T>
    {
        private static Lazy<T> _lazyInstance = new Lazy<T>(() =>
        {
            var constructor = typeof(T).GetConstructor(
                BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
                null, Type.EmptyTypes, null);
            if (constructor == null)
            {
                throw new InvalidOperationException(
                    $"{typeof(T).Name} must have a private parameterless constructor");
            }

            var instance = (T)constructor.Invoke(null);
            return instance;
        });
        public static T Instance => _lazyInstance.Value;
    }
}